# 作者: 赵若云
# 2025年02月20日21时23分57秒
# 3478783573@qq.com


def homework1(name, title="", gender=True):
    """
    多个缺省参数的传递练习，练习多个缺省参数
    :return:
    """
    if gender:
        gender_text = '男生'
    else:
        gender_text = '女生'

    print("%s%s 是 %s" % (title, name, gender_text))


def homework2(num, *arg, **kwargs):
    """
    多值参数练习，元组，字典的传参拆包练习
    :param num:
    :param arg:
    :param kwargs:
    :return:
    """
    print(num)
    print(arg)  # 元组
    print(kwargs)  # 字典


class Dog:
    """
    homework3
    设计一个类，实例化1个对象，会实现下面两种行为（和上课保持一致即可）
    需求
    •一只 黄颜色 的 狗狗 叫 小黄
    •具有  汪汪叫 行为
    •具有  摇尾巴 行为
    """

    def __init__(self, name, color):
        self.name = name
        self.color = color

    def bark(self):
        print('具有  汪汪叫 行为')

    def shake(self):
        print('具有  摇尾巴 行为')

    def __str__(self):
        return f'一只 {self.color} 的 狗狗 叫{self.name}'


# homework4 练习封装案例
class HouseItem:
    def __init__(self, name, area):
        self.name = name
        self.area = area

    def __str__(self):
        return "[%s] 占地 %.2f" % (self.name, self.area)


class House:
    def __init__(self, name, area):
        self.name = name
        self.area = area
        self.free_area = area
        self.HouseTtems = []

    def addHouseTtem(self, item):
        if self.free_area >= item.area:
            self.HouseTtems.append(item.name)
            self.free_area -= item.area
            print(f'添加了{item.name}')
        else:
            print('空间不足')

    def __str__(self):
        return ("户型：%s\n总面积：%.2f[剩余：%.2f]\n家具：%s"
                % (self.name, self.area,
                   self.free_area, self.HouseTtems))


# homework5 练习私有属性和私有方法
class Women:

    def __init__(self, name, age):
        self.name = name
        self.__age = age  # 私有属性

    # 私有方法
    def __secret(self):
        print(f'{self.name}的年龄是{self.__age}')

    def boyfriend(self):
        self.__secret()


# homework6 练习单继承，多重继承案例
class Animal:
    def __init__(self, name):
        self.name = name

    def eat(self):
        print('动物eat')

    def drink(self):
        print('动物drink')

    def sleep(self):
        print('动物sleep')

    def run(self):
        print('动物run')


class AnimalDog(Animal):  # 单继承
    def __init__(self, name, color):
        super().__init__(name)
        self.color = color

    def bark(self):
        print('狗bark')

    def use(self):
        return 'AnimalDog'


class Weapon:
    def __init__(self, weapon_name):
        self.weapon_name = weapon_name

    def use_weapon(self):
        print(f'使用{self.weapon_name}')

    def use(self):
        return 'Weapon'


class XiaoTian(AnimalDog, Weapon):  # 多重继承
    def __init__(self, name, color, weapon):
        AnimalDog.__init__(self, name, color)
        Weapon.__init__(self, weapon)

    def run(self):
        print(f'{self.name}fly')


# homework7 实现单例模式
class MusicPlayer:
    instance = None

    def __new__(cls, *args, **kwargs):  # cls代表的是类本身，self代表的是实例对象
        if cls.instance is None:
            cls.instance = object.__new__(cls)  # 分配空间，此时instance不是None了
            print("分配空间")
        return cls.instance

    def __init__(self, name):
        self.name = name
        print(f'初始化{self.name}')


if __name__ == '__main__':
    # homework1
    homework1('小明')
    homework1('小王', '班长')
    homework1('小红', gender=False)
    print('-' * 50)

    # homework2
    homework2(1, 2, 3, 4, 5, name="小明", age=18, gender=True)
    nums = (2, 3, 4, 5)
    xiaoming = {"name": "小明", "age": 18, "gender": True}
    homework2(2, *nums, **xiaoming)
    print('-' * 50)

    # homework3
    dog = Dog('小黄', '黄颜色')
    print(dog)
    dog.bark()
    dog.shake()
    print('-' * 50)

    # homework4
    bed = HouseItem("席梦思", 4)
    chest = HouseItem("衣柜", 2)
    table = HouseItem("餐桌", 1.5)

    house = House('三室一厅', 70)
    print(house)
    house.addHouseTtem(bed)
    house.addHouseTtem(chest)
    house.addHouseTtem(table)
    print(house)
    print('-' * 50)

    # homework5
    xiaomei = Women('小美', 18)
    xiaomei.boyfriend()
    print('-' * 50)

    # homework6
    dog = AnimalDog('大黄', '黄色')
    print(dog.name)
    print(dog.color)
    print('-' * 50)

    xiaotian = XiaoTian('啸天', '黑色', '翅膀')
    print(xiaotian.name)
    print(xiaotian.color)
    print(xiaotian.weapon_name)
    print('-' * 50)
    xiaotian.run()  # XiaoTian类中覆盖了
    xiaotian.bark()  # 来自AnimalDog
    xiaotian.use_weapon()  # 来自Weapon
    print(xiaotian.use())  # 都有的use，优先去AnimalDog找
    print('-' * 50)

    # homework7
    player1 = MusicPlayer('七里香')
    player2 = MusicPlayer('东风破')
    print(player1)  # 先new再init
    print(player2)
